package com.amazon.spring8amazon.controller;
import com.amazon.spring8amazon.common.QinWordsDelete;
import com.amazon.spring8amazon.common.Result;
import com.amazon.spring8amazon.domain.Params;
import com.amazon.spring8amazon.domain.QinWords;
import com.amazon.spring8amazon.mapper.ProductMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.amazon.spring8amazon.common.QinWordsDelete.*;

/**
 * @author cep123
 * @date 2024/9/5 18:36
 */



@RestController
@RequestMapping("/product")
//@MapperScan("com.amazon.spring8amazon.mapper")
@DS("localCk")
public class QinWordsController {


    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/qin")
    public Map<String,Object> getDangerousWords(String checkContent) throws Exception {

        //System.out.println(checkContent);

        // 获取侵权词列表
        List<QinWords> qinWordsListObj = productMapper.selectDangerousWords();

        // 从实体类中获取侵权词转换成列表
        List<String> qinWordsList = new ArrayList<>();
        for (QinWords qinWords : qinWordsListObj) {
            // 转换为小写 且忽略符号
            qinWordsList.add(qinWords.getDangerousWords().toLowerCase());
        }

        // 构建侵权单词查询列表
        List<String> resSingleWord = new ArrayList<>();
        List<String> resMultiWord = new ArrayList<>();
        // 单个单词的情况 和 多个单词的情况
        for (String word : qinWordsList) {
            if (word.split(" ").length >1){
                resMultiWord.add(" " + word.toLowerCase() + " ");
            } else {
                resSingleWord.add(word.toLowerCase()); // 前后加空格防止词根特例子 "S F" 干扰 fitness fun等等
            }

        }

        // 单个单词的情况
        Map<String, Object> singleWordMap = containsSingleWords( checkContent.toLowerCase(), resSingleWord);
        // 多个单词组成词组的检测(二次过滤检测)
        assert singleWordMap != null;
        Map<String, Object> finalResult = containsMultiWords(singleWordMap.get("Result").toString(), resMultiWord);

        // 定义最终结果输出
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("OldContent", checkContent);
        assert finalResult != null;
        resultMap.put("DeleteWord", finalResult.get("containsWords") + " " + singleWordMap.get("SingleContainsWords"));
        resultMap.put("NewContent", firstCharToUpperCase((String) finalResult.get("Result")));

        // 返回结果
        return resultMap;

    }


    @GetMapping("/tes")
    public Map<String,Object> getDangerousWordsByJson(String checkContent) throws Exception {

        try {

            // 获取侵权词列表
            List<QinWords> qinWordsListObj = productMapper.selectDangerousWords();
            List<String> qinWordsList = new ArrayList<>();
            for (QinWords qinWords : qinWordsListObj) {
                // 转换为小写 且忽略符号
                qinWordsList.add(qinWords.getDangerousWords().toLowerCase());
            }
//            System.out.println(qinWordsList);
            Map<String, Object> map = containsSingleWords(checkContent.toLowerCase(), qinWordsList);
//            map.put("qinWordsList", qinWordsList);
//            System.out.println(map);

            // 返回结果
            return map;
        } catch (Exception e) {
            Map<String, Object> map_error = new HashMap<>();
            map_error.put("error", e.getMessage());
            return map_error;
        }

    }






}